﻿<from class="layui-form layui-form-pane" action="" id="form-cloudnote">
    <input type="hidden" name="id">
    <input type="hidden" name="catalogId">
    <div class="layui-row" style="border-bottom: 1px solid #ccc;">
        <div class="layui-col-md9">
            <input type="text" name="name" style="border: 0px;"
                   placeholder="笔记标题" class="layui-input">
        </div>
        <div class="layui-col-md3" style="margin-top: -3px;text-align: right;padding-right: 10px;">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="note-add">保存
            </button>
            <div style="display: inline;top: 7px;margin-left: 10px;" class="menu-bars"><i
                    class="layui-icon layui-icon-menu"
                    style="font-size: 30px; color: #ccc;">&#xe614;</i>
                <dl class=" widget-menu">
                    <dd data-id="1" class="action-tool-history">历史</dd>
                    <dd data-id="1" class="action-tool-delete">删除</dd>
                    <dd data-id="1" class="action-tool-share">分享</dd>
                </dl>
            </div>
        </div>
    </div>
    <div>
        <textarea id="content" name="content" style="display: none;"></textarea>
    </div>
</from>
<script type="text/javascript">

    workUtils.onclick(".action-tool-history", function (e) {
        var id = workUtils.get("id");
        workUtils.open({url: "../web/note/history.html", title: "历史版本", width: '800px', height: "600px"})
    });
    workUtils.onclick(".action-tool-share", function (e) {
        var id = workUtils.get("id");
        workUtils.open({url: "../web/note/share.html", title: "分享"})
    });
    workUtils.onclick(".action-tool-delete", function (e) {
        var id = workUtils.get("id");
        workUtils.delete('../note/delete/' + id, function () {
            loadNote(catalogId);
        })
    });

    layui.define(['layer', 'form', 'layedit'], function (exports) {
        var form = layui.form;
        var idx = 0;
        var layedit = layui.layedit;

        function build() {
            var bh = $("#layui-body-note").height();
            idx = layedit.build('content', {
                uploadImage: {
                    url: '../note/upload' //接口url
                    , type: 'post' //默认post
                },
                height: (bh - 85)
            }); //建立编辑器
            workUtils.put("idx", idx);
        }

        var id = workUtils.get("id");
        var catalogId = workUtils.get("catalogId");
        $("[name='catalogId']").val(catalogId);
        if (id == undefined) {
            build();
        } else {
            $.getJSON("../note/get/" + id, function (data) {
                $("[name='name']").val(data.name);
                $("[name='id']").val(data.id);
                $("[name='catalogId']").val(data.catalogId);
                $("[name='content']").html(data.content);

                form.render(); //这个很重要
                build();
            });
        }


        form.on('submit(note-add)', function (data) {
            var index = layer.load(1);
            data.field["content"] = layedit.getContent(idx);
            setTimeout(function () {
                if (data.field.name == "") {
                    layer.msg("标题不能为空", {time: 1000});
                    layer.close(index);
                    return;
                }
                workUtils.ajaxPost('../note/add', data.field, function (result) {
                    layer.close(index);
                    loadNote(catalogId);
                    workUtils.msg(result);
                });
            }, 400);
            return false;
        });
    });
</script>